import os

import scrapy
from cardmi.items import NbaItem

class NBASpider(scrapy.Spider):
    name = 'nba'

    def start_requests(self):
        for i in range(ord('A'), ord('Z') + 1):
            url = 'https://china.nba.cn/stats2/league/playerlist.json?lastName={}&locale=zh_CN'.format(chr(i))
            yield scrapy.http.JsonRequest(url=url, callback=self.parse)

    def parse(self, response, **kwargs):
        players = response.json()['payload']['players']
        for player in players:
            name = player['playerProfile']['displayName']
            name_en = player['playerProfile']['displayNameEn']
            id = player['playerProfile']['playerId']

            # item = NbaItem()
            # item['id'] = id
            # item['name'] = name
            # yield item

            with open('players.txt', 'a+') as f:
                f.write(name_en + os.linesep)
